package com.me.study.javaCore.jndi;

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;

/**
 * JNDI 学习：服务工厂
 *
 * @author ME
 * @date   2020/5/14
 */
public class JndiServiceFactory implements ObjectFactory {

    @Override
    public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
        if (obj instanceof Reference) {
            JndiServiceImpl jndiService = new JndiServiceImpl();
            Reference ref = (Reference) obj;
            String field = (String) ref.get(JndiServiceImpl.class.getDeclaredFields()[0].getName()).getContent();
            jndiService.setPersonName(field);
            return jndiService;
        }
        return null;
    }
}
